package com.tourism.website.collection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tourism.website.collection.entity.Favorite;
import com.tourism.website.collection.mapper.FavoriteMapper;
import com.tourism.website.collection.service.IFavoriteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourismwebsite.collection.dto.FavoriteDTO;
import com.tourismwebsite.collection.query.FavoriteQuery;
import org.springframework.stereotype.Service;
import org.mapstruct.Mapper;
import com.tourismwebsite.dto.PageDTO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

// TODO 14转换器以及接口实现
@Mapper(componentModel="spring")
interface MsFavoriteMapper{
    FavoriteDTO  favoriteToFavoriteDTO(Favorite favorite);
}
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LYC
 * @since 2024-06-06
 */
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements IFavoriteService {
    @Resource
     MsFavoriteMapper msFavoriteMapper;
    @Resource
    FavoriteMapper favoriteMapper;

    @Override
    public List<FavoriteDTO> queryFavorite(Integer favoriteId) {
        List<Favorite> favorites=favoriteMapper.selectFavorite(favoriteId);
        List<FavoriteDTO> favoriteDTOs = new ArrayList<>();
        for(Favorite favorite : favorites) {
            FavoriteDTO favoriteDTO;
            favoriteDTO=msFavoriteMapper.favoriteToFavoriteDTO(favorite);
            favoriteDTOs.add(favoriteDTO);
        }
        return favoriteDTOs;
    }
    @Override
    public boolean saveFavorite(FavoriteQuery favoriteQuery) {
        int result=favoriteMapper.saveFavorite(favoriteQuery.getFavoriteId(),favoriteQuery.getArticleId().get(0));
        if(result>0) return true;
        return false;
    }

    @Override
    public boolean deleteBatchFavorite(FavoriteQuery favoriteQuery) {
                List<Favorite> favoriteList = new ArrayList<>();
// 根据请求的userId和articleId删除数据

        for (Integer articleId : favoriteQuery.getArticleId()) {
            System.out.println(favoriteQuery.getArticleId());
            Favorite favorite=new Favorite();
            favorite.setFavoriteId(favoriteQuery.getFavoriteId());
            favorite.setArticleId(articleId);
            favoriteList.add(favorite);
        }
        int result=favoriteMapper.deleteBatchFavorite(favoriteList);
        if(result>0) return true;
        return false;
    }
}
